package com.cloud.punish.workflow.command.cancel;

import java.util.Date;

import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import com.cloud.punish.common.context.HumanSession;
import com.cloud.punish.common.utils.SessionHelper;
import com.cloud.punish.workflow.command.Command;
import com.cloud.punish.workflow.command.WfRequest;
import com.cloud.punish.workflow.command.WfResponse;
import com.cloud.punish.workflow.entity.WfActInst;
import com.cloud.punish.workflow.entity.WfItemInst;
import com.cloud.punish.workflow.entity.WfPerPart;
import com.cloud.punish.workflow.enums.WfActStateEnum;
import com.cloud.punish.workflow.enums.WfItemTypeEnum;
import com.cloud.punish.workflow.enums.WfRecStateEnum;
import com.cloud.punish.workflow.mapper.WfActInstMapper;
import com.cloud.punish.workflow.mapper.WfItemInstMapper;
import com.cloud.punish.workflow.mapper.WfPerPartMapper;


/**
 * 案件作废指令
 * @author Minglong.tao
 *
 */
public class CancelCommand extends Command{

	@Override
	public void execute(WfRequest request, WfResponse resp, ApplicationContext context) {
		CancelRequest cancelReq = (CancelRequest) request;
		HumanSession humanSession = SessionHelper.get();

		// 设置流程实例为已处理
		if(cancelReq.getActId() > 0){
			//在启用工作流，存在act_id的情况下才进行工作流记录
			WfActInstMapper wfActInstMapper = context.getBean(WfActInstMapper.class);
			WfActInst actInst = wfActInstMapper.selectByPrimaryKey(cancelReq.getActId());
			actInst.setActId(cancelReq.getActId());
			actInst.setOpinion(cancelReq.getOpinion());
			actInst.setActStateId(WfActStateEnum.PROCESSED.getActStateId());
			actInst.setActStateName(WfActStateEnum.PROCESSED.getActStateName());
			wfActInstMapper.updateByPrimaryKeySelective(actInst);

			// 工作项表
			WfItemInstMapper wfItemInstMapper = context.getBean(WfItemInstMapper.class);
			WfItemInst itemInst = new WfItemInst();
			itemInst.setActId(actInst.getActId());
			itemInst.setActDefName(actInst.getActDefName());
			itemInst.setActionTime(new Date());
			itemInst.setActionName("作废");
			itemInst.setDetail(humanSession.getHumanName() + "在" + actInst.getActDefName() + "阶段办理作废");
			itemInst.setItemContent(cancelReq.getOpinion());
			itemInst.setHumanId(humanSession.getHumanId());
			itemInst.setHumanName(humanSession.getHumanName());
			itemInst.setItemTypeId(WfItemTypeEnum.CANCEL.getItemTypeId());
			itemInst.setItemTypeName(WfItemTypeEnum.CANCEL.getItemTypeName());
			wfItemInstMapper.insert(itemInst);

			// 经办人表
			WfPerPartMapper wfPerPartMapper = context.getBean(WfPerPartMapper.class);
			WfPerPart perPart = new WfPerPart();
			perPart.setHumanId(humanSession.getHumanId());
			perPart.setRecId(cancelReq.getRecId());
			if (!wfPerPartMapper.existsWithPrimaryKey(perPart)) {
				wfPerPartMapper.insert(perPart);
			}
		}

		// 设置案卷状态为办结
		JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
		jdbcTemplate.update("update rec_punish set state_id = ?, state_name = ? where rec_id = ?",
				WfRecStateEnum.CANCEL.getStateId(), WfRecStateEnum.CANCEL.getStateName(), cancelReq.getRecId());
	
		
		CancelResponse cancelResp = (CancelResponse) resp;
		cancelResp.setRecId(cancelReq.getRecId());
	}

}
